#!/bin/sh

if [ -z "$1" ]; then
	echo "Usage $0 patch_count"
	exit
fi

set -x -e -u

patch_count=$1
in_dir=../training/normal
out_dir=../PATCH

for img in "$in_dir"/*.tif
do
	# img like ../training/normal/tumor_xxxx.tif
	# idx like normal_xxx
	idx=$(basename -s .tif "$img")
	tissue_mask=$out_dir/tissue_mask/$idx.npy
	normal_spot=$out_dir/normal_spot/$idx.spot

	[ -f "$tissue_mask" ] || \
	python ./camelyon16/bin/tissue_mask.py --level 6 "$img"  "$tissue_mask"

	rm -f "$normal_spot"
	python ./camelyon16/bin/sampled_spot_gen.py "$tissue_mask" "$normal_spot" "$patch_count"

	python ./camelyon16/bin/patch_gen.py --level 0 --num_process 32 --patch_size 224 "$img" "$normal_spot" $out_dir/training_patch/normal/"$idx"/
done

cd $out_dir/training_patch
find normal -type f -name '*.jpg' | tee normal.list
wc -l normal.list

sed -i "s@\"data_path_train\".*@\"data_path_train\": \"$out_dir/training_patch\",@g" camelyon16/configs/cnn.json
sed -i "s@\"data_path_valid\".*@\"data_path_valid\": \"$out_dir/training_patch\",@g" camelyon16/configs/cnn.json

cat camelyon16/configs/cnn.json
